import {transformAsync} from "@babel/core"
import parser from "@babel/parser"
import fs from "fs"
// https://blog.csdn.net/m0_73689941/article/details/137397980
// traverse is not a function
// import _traverse from "@babel/traverse";
// const traverse = _traverse.default;
import traverse from "@babel/traverse";
import _generate from "@babel/generator";
import path from "path";
const generate = _generate.default;
//https://blog.csdn.net/weixin_42522953/article/details/112515285
const code = `function hello(){
    return 'hello world'
}`
const srcDir = path.resolve("src/babel")
const fileContent = fs.readFileSync(path.join(srcDir,"./testFunc.js"),{encoding:"utf-8"})
const ast = await parser.parse(fileContent)
traverse(ast,{
    // IfStatement(path){
    //     console.log(path.node)
    // },
    // FunctionDeclaration(path)  {
    //     // console.log(path.node)
    // },
    'FunctionDeclaration|VariableDeclaration'(path)  {
        // console.log(path.node)
    },
    enter(path){
        if(path.isFunctionDeclaration()){
            // console.log(path.scope)
            const methordName = path.node.id.name
            console.log(path.node.body)
            // path.isMemberExpression()
            // const hasComment = hasCommentLine(path)
            // console.log(hasComment)
        }
        if(path.isReturnStatement()){
            // console.log("return value",JSON.stringify(path.node.argument))
        }
    }
})

function hasDocComment(path){
    if(path.node.leadingComments){
        const comment = path.node.leadingComments.filter(comment=>comment.type === "CommentBlock").map(comment=>comment.value)
        return comment
    }else{
        return []
    }
}
function hasCommentLine(path){
    if(path.node.leadingComments){
        const comment = path.node.leadingComments.filter(comment=>comment.type === "CommentLine").map(comment=>comment.value)
        return comment
    }else{
        return []
    }
}
generate(ast)
